package com.start.form.engine.core;


import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.start.common.tools.StartStreamTools;
import com.start.form.engine.core.model.StartFormColumnModel;
import com.start.form.engine.core.vo.StartFormColumnVo;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StartColumnInfo implements StartFormEngineConstant.FormBaseColumns {
    @Getter
    private List<StartFormColumnModel> addLists = new ArrayList<>();
    @Getter
    private List<StartFormColumnVo> editColumns = new ArrayList<>();
    @Getter
    private List<StartFormColumnModel> deleteColumns = new ArrayList<>();

    public List<String> getDeleteColumnIds() {
        if (deleteColumns == null || deleteColumns.size() < 1) return new ArrayList<>();
        return deleteColumns.stream().map(StartFormColumnModel::getId).collect(Collectors.toList());
    }


    private final List<StartFormColumnModel> oldColumn;
    private final List<StartFormColumnModel> newColumn;
    private static final List<String> IGNORE_COL_TYPES = new ArrayList<>();

    static {
        IGNORE_COL_TYPES.add("title");
        IGNORE_COL_TYPES.add("dynamic");
    }


    public StartColumnInfo(JSONArray newColumn, List<StartFormColumnModel> oldColumn, boolean throwRepeat) {
        this.newColumn = newColumn.toJavaList(StartFormColumnModel.class).stream().filter(e -> !IGNORE_COL_TYPES.contains(e.getType())).collect(Collectors.toList());
        this.oldColumn = oldColumn;
        analysis();
    }

    public StartColumnInfo(JSONObject newConfigColumn, JSONObject oldConfigColumn, boolean throwRepeat) {
        newColumn = newConfigColumn.getJSONArray("column").toJavaList(StartFormColumnModel.class);
        this.oldColumn = oldConfigColumn.getJSONArray("column").toJavaList(StartFormColumnModel.class);
        analysis();

    }

//    public static void main(String[] args) {
//        String oldStr = FileUtil.readUtf8String("E:\\gxl\\yudao-cloud-form\\yudoao-cloud-form\\yudoao-cloud-form-core\\src\\main\\java\\cn\\iocoder\\cloud\\form\\core\\oldConfig.json");
//        JSONObject oldConfig = JSONObject.parseObject(oldStr);
//        String newStr = FileUtil.readUtf8String("E:\\gxl\\yudao-cloud-form\\yudoao-cloud-form\\yudoao-cloud-form-core\\src\\main\\java\\cn\\iocoder\\cloud\\form\\core\\newConfig.json");
//        JSONObject newConf = JSONObject.parseObject(newStr);
//        StartColumnInfo startColumnInfo = new StartColumnInfo(new StartFormConfig(), newConf, oldConfig, false);
//        List<StartFormColumn> add = startColumnInfo.getAddLists();
//        List<StartFormColumnVo> edit = startColumnInfo.getEditColumns();
//        List<StartFormColumn> del = startColumnInfo.getDeleteColumns();
//        System.out.println("da");
//    }

    /**
     * 解析列
     */
    private void analysis() {
        Map<String, StartFormColumnModel> newColMap = newColumn.stream().filter(StartStreamTools.distinctByKey(StartFormColumnModel::getProp)).collect(Collectors.toMap(StartFormColumnModel::getProp, e -> e));
        Map<String, StartFormColumnModel> oldColMap = oldColumn.stream().filter(StartStreamTools.distinctByKey(StartFormColumnModel::getProp)).collect(Collectors.toMap(StartFormColumnModel::getProp, e -> e));
        deleteColumns = oldColumn.stream().filter(e -> !newColMap.containsKey(e.getProp())).collect(Collectors.toList());
        addLists = newColumn.stream().filter(e -> !oldColMap.containsKey(e.getProp())).collect(Collectors.toList());
        Map<String, StartFormColumnModel> addMap = addLists.stream().collect(Collectors.toMap(StartFormColumnModel::getProp, e -> e));

        for (StartFormColumnModel col : newColumn) {
            if (addMap.containsKey(col.getProp())) {
                continue;
            }
            String prop = col.getProp();
            StartFormColumnModel oldCol = oldColMap.get(prop);
            if (oldCol == null) continue;
            StartFormColumnModel newCol = newColMap.get(prop);
            if (newCol == null) continue;
            if (checkColIsTheSame(newCol, oldCol)) {
                continue;
            }
            StartFormColumnVo editCol = JSONObject.parseObject(JSONObject.toJSONString(newCol), StartFormColumnVo.class);
            editCol.setOldProp(oldCol.getProp());
            editColumns.add(editCol);
        }

    }

    /**
     * 检查列是否相同
     *
     * @param newCol
     * @param oldCol
     * @return true
     */
    private boolean checkColIsTheSame(StartFormColumnModel newCol, StartFormColumnModel oldCol) {
        if (!newCol.getLabel().equals(oldCol.getLabel())) { //检查label
            return false;
        }
        if (!newCol.getSqlType().equals(oldCol.getSqlType())) { //检查数据类型
            return false;
        }
        //检查数据长度
        if (newCol.getSqlTypeDataLength() == null && oldCol.getSqlTypeDataLength() == null) {
            return true;
        }
        if (newCol.getSqlTypeDataLength() == null) {
            return false;
        }
        if (oldCol.getSqlTypeDataLength() == null) {
            return false;
        }
        return newCol.getSqlTypeDataLength().equals(oldCol.getSqlTypeDataLength());
    }


}
